package ip

import (
	"fmt"
	"math/big"
	"net"
	"strings"
)

type SIP插件 struct {
	S *Searcher
}

func F新建ip查询(path string) (sip *SIP插件, err error) {
	sip = &SIP插件{}
	var cBuff []byte
	cBuff, err = LoadContentFromFile(path)
	if err != nil {
		return
	}
	sip.S, err = NewWithBuffer(cBuff)
	return
}

func (a *SIP插件) F获取省市区(ip string) (info []string, err error) {
	region, err := a.S.SearchByStr(ip)
	if err != nil {
		return
	}
	info = strings.Split(region, "|")
	return
}

func Fip转换成int(ip string) int64 {
	ret := big.NewInt(0)
	ret.SetBytes(net.ParseIP(ip).To4())
	return ret.Int64()
}

func F数字转ip(ip int64) string {
	return fmt.Sprintf("%d.%d.%d.%d",
		byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip))
}

func Fip转字符串(ip interface{}) (ipadd string) {
	ips, b := ip.(int64)
	if b {
		ipadd = F数字转ip(ips)
	} else {
		ipadd = ip.(string)
	}
	return
}
